Imports IICBusinessLogic.PeriodProcessing
Imports CrystalReportViewer
Imports System.ComponentModel
Imports org.codegen.common.ProgressIndicator
Imports System.Security.Principal

''' <summary>
''' UI related function to Processes a period
''' </summary>
''' <remarks></remarks>
Public Class UIProcessPeriod

    Const REPORT_KEY_DIPLOMA_HOLDERS As String = "7"

    Private progress As BackroundProgressIndicator.BackroundProgressIndicator

    Private Property period As Period
    Private Property currentUser As IPrincipal
    Private Property periodProcessor As PeriodProcessor
    Private Property backgroundWorker As BackgroundWorker

    Public Sub processPeriod(ByVal period As Period)

        Me.period = period

        'ask user to confirm
        If winUtils.MsgboxQuestion( _
                "Are you sure you want to Process period " & Me.period.PeriodDescription & " for earned Diplomas/Certificates?") _
                                <> MsgBoxResult.Yes Then Exit Sub

        Me.currentUser = ModelContext.Current.CurrentUser

        Me.progress = New BackroundProgressIndicator.BackroundProgressIndicator
        Me.progress.progressWindowTitle = "Processing Period " & Me.period.PeriodDescription
        Me.progress.showCancel = True
        Me.progress.Start(AddressOf progressProcessPeriod)

    End Sub

    Private Sub progressProcessPeriod(ByVal sender As Object, ByVal e As DoWorkEventArgs)

        Me.periodProcessor = New PeriodProcessor(Me.period)
        Me.backgroundWorker = CType(sender, BackgroundWorker)

        AddHandler Me.periodProcessor.processGoing, AddressOf processPeriodProgress
        AddHandler Me.periodProcessor.processFinished, AddressOf processPeriodFinished

        ModelContext.newCurrent(Me.currentUser)
        Me.periodProcessor.processPeriodAndStoreToDatabase()

    End Sub

    Private Sub processPeriodProgress(ByVal sender As Object, _
                        ByVal e As ProgressableEventArgs)

        Me.backgroundWorker.ReportProgress( _
               CInt((e.currentStep / e.totalSteps) * 100), _
               e.progressMessage)

        If Me.backgroundWorker.CancellationPending Then
            e.wasCancelled = True
            Me.backgroundWorker.CancelAsync()

        End If

    End Sub

    Private Sub processPeriodFinished(ByVal sender As Object, _
                                      ByVal e As ProgressableEventArgs)

        Call Me.progress.EndProcess()

        If Me.periodProcessor.certificatesEarnedCount > 0 Or Me.periodProcessor.diplomasEarnedCount > 0 Then
            Call winUtils.MsgboxInfo( _
                     String.Format(
                     "Period #{0} Processed for diplomas and certifications." & vbCrLf & _
                     "There were {1} Members (Students) processed, {2} diploma/s and {3} certificate/s earned. ", _
                                Me.periodProcessor.period.PeriodDescription, _
                                Me.periodProcessor.membersProcessedCount, _
                                Me.periodProcessor.diplomasEarned.Count, _
                                Me.periodProcessor.certificatesEarnedCount))


            Dim r As ReportMatadata = My.Application.reportsCache.getSystemReportByKey(REPORT_KEY_DIPLOMA_HOLDERS)
            Dim cr As CRReportViewer = New CRReportViewer
            cr.ReportMetaData = r
            cr.setCriterionValue("PeriodId", Me.period.PeriodId)
            cr.ShowReport()

        Else
            Call winUtils.MsgboxInfo( _
            String.Format("Period #{0} Processed for diplomas and certifications." & vbCrLf & _
             "There were {1} Members (Students) processed, and no diplomas or certificates earned. ", _
                          Me.periodProcessor.period.PeriodDescription, _
                          IIf(Me.periodProcessor.membersProcessedCount = 0, "No", Me.periodProcessor.membersProcessedCount)))
        End If

    End Sub

End Class